import { NextRequest } from 'next/server';
import { withMiddleware } from '@/lib/middleware/entry';
import { prisma } from '@/lib/prisma';
import { authenticateRequest } from '@/utils/auth';
import { successResponse, errorResponse } from '@/lib/response';

// POST /api/admin/friendly-links/create - 创建友情链接
async function handler(req: NextRequest) {
  const authResult = await authenticateRequest(req);
  if (!authResult.success) {
    return errorResponse(authResult.error || '身份验证失败');
  }

  const { name, url, order = 0, isActive = 1 } = await req.json();

  if (!name || !url) {
    return errorResponse('名称与链接为必填项');
  }

  const created = await prisma.friendlyLink.create({
    data: {
      name,
      url,
      order: Number(order) || 0,
      isActive: Number(isActive) || 1,
    },
  });

  return successResponse(created, '创建成功');
}

export const POST = withMiddleware(handler);